<?php defined('BASEPATH') OR exit('No direct script access allowed');
/**
 * Description: 社区
 *
 * @author      Eden
 * @datetime    2016/12/10 23:51
 * @copyright   Beijing CmsTop Technology Co.,Ltd.
 */

class Slider_Model extends ADM_Model
{

    /**
     * 初始化
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * @param int $offset
     * @param int $limit
     * @param array $where
     */
    public function getLists($offset = 0, $limit = 20, $where = array())
    {
        $pgDb = clone($this->db);
        $total = $pgDb->count_all_results('bbs_slider');
        if (empty($total)) {
            return [
                'total' => 0,
                'lists' => []
            ];
        }

        $db = clone($this->db);
        if (!empty($where)) {
            $db->where($where);
        }
        $db->limit($limit, $offset);
        $db->order_by('created', 'DESC');
        $query = $db->get('bbs_slider');
        $rows = $query->result_array();
        return [
            'total' => $total,
            'lists' => $rows
        ];
    }

    /**
     * @param $posts
     */
    public function submit($posts)
    {
        $types = [
            'link' => 1,
            'base' => 2,
            'product' => 3
        ];

        $db = clone($this->db);
        $db->insert('bbs_slider', [
            'title' => value($posts, 'title', ''),
            'source' => value($posts, 'source', ''),
            'thumb' => value($posts, 'thumb', ''),
            'createdby' => adminId(),
            'timestart' => value($posts, 'timestart', ''),
            'timeend' => value($posts, 'timeend', ''),
            'type' => $types[$posts['type']],
            'value' => value($posts, 'value', ''),
            'status' => value($posts, 'status', 1)
        ]);
        return $db->insert_id();
    }

    /**
     * @param $posts
     */
    public function edit($posts)
    {
        $types = [
            'link' => 1,
            'base' => 2,
            'product' => 3
        ];

        $db = clone($this->db);
        $db->where('id', $posts['id']);
        unset($posts['id']);
        $db->update('bbs_slider', [
            'title' => value($posts, 'title', ''),
            'source' => value($posts, 'source', ''),
            'thumb' => value($posts, 'thumb', ''),
            'createdby' => adminId(),
            'timestart' => value($posts, 'timestart', ''),
            'timeend' => value($posts, 'timeend', ''),
            'type' => $types[$posts['type']],
            'value' => value($posts, 'value', ''),
            'status' => value($posts, 'status', 1)
        ]);
        return $db->affected_rows();
    }

    /**
     * 获取详情
     * @param $id
     * @return mixed
     */
    public function detail($id)
    {
        $db = clone($this->db);
        $query = $db->get_where('bbs_slider', ['id' => $id]);
        return $query->row_array();
    }

    /**
     * 删除
     * @param $id
     * @return mixed
     */
    public function deleteItem($id)
    {
        $db = clone($this->db);
        $db->where('id', $id);
        $db->delete('bbs_slider');
        return $db->affected_rows();
    }

    /**
     * 修改状态
     * @param $id
     * @return mixed
     */
    public function changeStatus($id)
    {
        $db = clone($this->db);
        $query = $db->get_where('bbs_slider', ['id' => $id]);
        $row = $query->row_array();
        if (!$row) {
            return false;
        }

        $db->where('id', $id);
        $db->update('bbs_slider', [
            'status' => $row['status'] == 1 ? 0 : 1
        ]);
        return $db->affected_rows();
    }

}
